package com.yidu.t299.controller.ljc;
import com.yidu.t299.domain.ljc.Instoreroom;
import com.yidu.t299.service.ljc.InstoreroomBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 刘建常
 * @version 1.0
 * @description
 * @date:2023/3/10 10:23
 */
@RestController
public class InstoreroomController {
    @Autowired
    private InstoreroomBiz instoreroomBiz;

    @GetMapping("instoreroom/restful_instoreroom")
    public Map<String, Object> findAll(int limit, int page, Instoreroom instoreroom) {
        // 根据school对象得到条件字符串
        String condition = instoreroomBiz.getCondition(instoreroom);
        // 按条件统计总记录行数
        int count = instoreroomBiz.selectCount(condition);
        // 按条件分页查询
        List<Instoreroom> instoreroomList = instoreroomBiz.selectByPage(limit, page-1, condition);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为校区对象集合的映射
        mapData.put("data", instoreroomList);
        //把映射集合响应到客户端
        return mapData;
    }

    @PostMapping("instoreroom/restful_instoreroom")
    public String addBook(@RequestBody Instoreroom instoreroom) {
        //调用业务层实现新增员校区，并返回正确与否字符串
        return instoreroomBiz.insert(instoreroom) > 0 ? "add-success" : "add-failure";
    }

    @PutMapping("instoreroom/restful_instoreroom")
    public String updateBook(@RequestBody Instoreroom instoreroom) {
        //调用业务层实现修改校区，并返回正确与否字符串
        return instoreroomBiz.updateByPrimaryKey(instoreroom) > 0 ? "update-success" : "update-failure";
    }

    @DeleteMapping("instoreroom/restful_instoreroom/{inId}")
    public String deleteBookById(@PathVariable("inId") int inId) {
        //调用业务层实现删除校区，并返回正确与否字符串
        return instoreroomBiz.deleteByPrimaryKey(inId) > 0 ? "delete-success" : "delete-failure";
    }

    @PutMapping("instoreroom/batchDel")
    public String deleteBookByManyId(@RequestParam String inIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = inIdStr.split(",");
        //循环按校区编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (instoreroomBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "delete-failure";
            }
        }
        return "delete-success";
    }
}
